package cn.myframe.service.tb.impl;

import org.springframework.stereotype.Service;

import cn.myframe.dao.tb.TbUserDao;
import cn.myframe.dao.tb.TbUserRegionDao;
import cn.myframe.entity.tb.TbUserEntity;
import cn.myframe.service.tb.TbUserService;
import cn.myframe.service.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Service("tbUserService")
public class TbUserServiceImpl extends BaseServiceImpl<TbUserDao,TbUserEntity> implements TbUserService {

    @Resource
	TbUserRegionDao tbUserRegionDao;

    @Transactional(rollbackFor = Exception.class)
	@Override
    public boolean insert(TbUserEntity tbUserEntity){
        super.insert(tbUserEntity);
        tbUserEntity.setTbRoleList(null);
        List<Long> list = tbUserEntity.getTbRoleIdsList();
        if(list != null && list.size()>0){
        	  tbUserRegionDao.saveUserId(tbUserEntity.getUserId(),list);
        }    
		return true;
	}

	 @Transactional(rollbackFor = Exception.class)
	 @Override
	 public boolean deleteBatchIds(Collection<? extends Serializable> idList){
    	for(Serializable id : idList){
            tbUserRegionDao.deleteByUserId(id);
    	}
    	super.deleteBatchIds(idList);
    	return true;
	}
    
    @Transactional(rollbackFor = Exception.class)
	@Override
    public boolean updateById(TbUserEntity tbUserEntity){
        tbUserRegionDao.deleteByUserId(tbUserEntity.getUserId());
        tbUserEntity.setTbRoleList(null);
        List<Long> list = tbUserEntity.getTbRoleIdsList();
        if(list != null && list.size()>0){
        	  tbUserRegionDao.saveUserId(tbUserEntity.getUserId(),list);
        }
    	super.updateById(tbUserEntity);
    	return true;
    }
}

